#!/bin/bash

function usage()   #show how to use this shell
{
    printf "usage: bash FILE.SH \033[32m[-h|--help] [-b4|--bond4] [-vlan|--setvlan] [-net|--setnet]\033[0m \n"
    printf "                    \033[32m[-b4-ip|--bond4-ip] [-vlan-ip|--setvlan-ip] [-net-ip|--setnet-ip]\033[0m \n\n"
    printf "optional arguments:\n"
    printf "\033[32m  -h, --help              Search for help information\033[0m\n"
    printf "\033[32m  -b4, --bond4            Configure the bond4 interface\033[0m\n"
    printf "\033[32m  -vlan, --setvlan        Configure the vlan SubInterface\033[0m\n"
    printf "\033[32m  -net, --setnet          Configure the Interface\033[0m\n"
    printf "\033[32m  -ip                     Configure IP/NETMASK/GATEWAY\033[0m\n\n"
    exit 0
}

function echo_col()  #echo use colour
{
    echo -e "\033[32m "$1" \033[0m"
}

function if_exist()  #network card exists
{
    if [ ! -d /sys/class/net/${1} ];
    then
        echo_col "${1} not exist.";
        usage
    fi
}

function set_network()
{
    read -p "Input the network card name: " network_card_name

    ifdown ${network_card_name} >/dev/null 2>&1
    mv -f /etc/sysconfig/network-scripts/ifcfg-"${network_card_name}" \
          /etc/sysconfig/network-scripts/ifcfg-"${network_card_name}".bak \
          || echo "skip backup ${network_card_name}"

cat << EOF >/etc/sysconfig/network-scripts/ifcfg-${network_card_name}
TYPE="Ethernet"
BOOTPROTO="none"
NAME="${network_card_name}"
DEVICE="${network_card_name}"
ONBOOT="yes"
EOF
}

function set_interface()
{
    ifdown ${1} >/dev/null 2>&1
    mv -f /etc/sysconfig/network-scripts/ifcfg-"${1}" \
          /etc/sysconfig/network-scripts/ifcfg-"${1}".bak \
          || echo "skip backup ${1}"

cat << EOF >/etc/sysconfig/network-scripts/ifcfg-${1}
TYPE=Ethernet
DEVICE=${1}
ONBOOT=yes
BOOTPROTO=none
MASTER=${2}
SLAVE=yes
EOF
}

function create_bond()
{
cat <<EOF >/etc/sysconfig/network-scripts/ifcfg-${bond_name}
TYPE=Bond
DEVICE=${bond_name}
ONBOOT=yes
BOOTPROTO=none
BONDING_OPTS="mode=802.3ad miimon=100 lacp_rate=fast"
EOF
}

function create_vlan()
{
cat <<EOF >/etc/sysconfig/network-scripts/ifcfg-${bond_name}.${vlan_id}
DEVICE=${bond_name}.${vlan_id}
BOOTPROTO="static"
ONBOOT=yes
VLAN=yes
EOF
}

function set_ip()
{
    read -p "IPADDR: " ipaddr
    read -p "NETMASK: " netmask
    read -p "GATEWAY: " gateway

cat <<EOF >>/etc/sysconfig/network-scripts/ifcfg-${1}
IPADDR=$ipaddr
NETMASK=$netmask
EOF

    if  [ -n "$gateway" ];then
cat <<EOF >>/etc/sysconfig/network-scripts/ifcfg-${1}
GATEWAY=$gateway
EOF
    fi
}

function set_bond()
{
    read -p "Input the network card name1: " network_card_name1
    read -p "Input the network card name2: " network_card_name2
    read -p "Input the bond name: " bond_name

    if_exist "$network_card_name1"
    if_exist "$network_card_name2"
    set_interface "$network_card_name1" "$bond_name"
    set_interface "$network_card_name2" "$bond_name"
    create_bond "$bond_name"
}

function set_vlan()
{
    read -p "Input the vlan's bond name: " bond_name
    read -p "Input the vlan id: " vlan_id

    mv -f /etc/sysconfig/network-scripts/ifcfg-"${bond_name}.${vlan_id}" \
          /etc/sysconfig/network-scripts/ifcfg-"${bond_name}.${vlan_id}".bak \
          || echo "skip backup ifcfg-${bond_name}.${vlan_id}"
    create_vlan ${bond_name} ${vlan_id}
}

:<<!
-----------------------------------------The following is the main----------------------------------------
!

if [[ $# < 1 ]];then
    printf "Try 'bash [FILE.SH] --help' for more information.\n"
    exit
fi

while [ $# -gt 0 ]
do
    case $1 in
	-h|--help)
        usage
	;;
	-b4|--bond4)
        set_bond
        ifdown $bond_name && ifup $bond_name
        ifdown $network_card_name1 && ifup $network_card_name1
        ifdown $network_card_name2 && ifup $network_card_name2
	;;
	-b4-ip|--bond4-ip)
        set_bond
        set_ip "$bond_name"
        ifdown $bond_name && ifup $bond_name
        ifdown $network_card_name1 && ifup $network_card_name1
        ifdown $network_card_name2 && ifup $network_card_name2
	;;
	-vlan|--setvlan)
        set_vlan
        printf "\n\033[32mPlease restart vlan's network.\033[0m\n"
	;;
    -vlan-ip|--setvlan-ip)
        set_vlan
        set_ip "${bond_name}.${vlan_id}"
        printf "\n\033[32mPlease restart vlan's network.\033[0m\n"
    ;;
    -net|--setnet)
        set_network
        printf "\n\033[32mPlease restart network.\033[0m\n"
    ;;
    -net-ip|--setnet-ip)
        set_network
        set_ip "${network_card_name}"
        printf "\n\033[32mPlease restart network.\033[0m\n"
    ;;
	*)
	    printf "Error: Unknow Options -> $*"
        printf "Try 'bash [FILE.SH] --help' for more information.\n"
	    exit 1
	;;
    esac
    shift
done
